package com.pro.common.service.config;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.pro.framework.api.FrameworkConst;
import com.pro.framework.api.enums.EnumJsonObjectMapperStrategy;
import com.pro.framework.api.enums.IEnumInt;
import com.pro.framework.api.util.JSONUtils;
import com.pro.framework.json.service.FlexibleLocalDateTimeDeserializer;
import com.pro.framework.json.util.ObjectMapperBuilder;
import com.pro.framework.spring.util.ClassScanner;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.time.LocalDateTime;

@Configuration
public class JsonConfig {


    /**
     * 默认ObjectMapper - MVC策略（替代原来的objectMapper）
     */
    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = mvcObjectMapper();
        JSONUtils.init(objectMapper);
        return objectMapper;
    }
    /**
     * 默认ObjectMapper - MVC策略（替代原来的objectMapper）
     */
    @Bean
    @Qualifier(FrameworkConst.JsonObjectMapper.mvcObjectMapper)
    public ObjectMapper mvcObjectMapper() {
        return createObjectMapper(EnumJsonObjectMapperStrategy.MVC);
    }

    /**
     * 缓存ObjectMapper - 带类型信息
     */
    @Bean
    @Qualifier(FrameworkConst.JsonObjectMapper.cacheObjectMapper)
    public ObjectMapper cacheObjectMapper() {
        return createObjectMapper(EnumJsonObjectMapperStrategy.CACHE);
    }
    /**
     * 缓存ObjectMapper - 带类型信息
     */
    @Bean
    @Qualifier(FrameworkConst.JsonObjectMapper.cacheSimpleObjectMapper)
    public ObjectMapper cacheSimpleObjectMapper() {
        return createObjectMapper(EnumJsonObjectMapperStrategy.CACHE_SIMPLE);
    }

    /**
     * 消息ObjectMapper - 不带类型信息
     */
    @Bean
    @Qualifier(FrameworkConst.JsonObjectMapper.messageObjectMapper)
    public ObjectMapper messageObjectMapper() {
        return createObjectMapper(EnumJsonObjectMapperStrategy.MESSAGE);
    }

    /**
     * 消息ObjectMapper - 不带类型信息
     */
    @Bean
    @Qualifier(FrameworkConst.JsonObjectMapper.multiTableQueryObjectMapper)
    public ObjectMapper multiTableQueryObjectMapper() {
        ObjectMapper objectMapper = createObjectMapper(EnumJsonObjectMapperStrategy.MULTI_TABLE_QUERY);

        // 时间灵活处理（你原有的flexibleDateTimeObjectMapper逻辑）
        SimpleModule flexibleDateTimeModule = new SimpleModule();
        flexibleDateTimeModule.addDeserializer(LocalDateTime.class, new FlexibleLocalDateTimeDeserializer());
        objectMapper.registerModule(flexibleDateTimeModule);
        return objectMapper;
    }


    /**
     * 根据策略创建ObjectMapper（整合你原有的所有配置）
     */
    public static ObjectMapper createObjectMapper(EnumJsonObjectMapperStrategy strategy) {
        ObjectMapper objectMapper = new ObjectMapper();

        // 基础配置（你原有的配置）
        ObjectMapperBuilder.applyBaseConfiguration(objectMapper, ClassScanner.scanClasses("com.pro.**.enums", IEnumInt.class));

        // 根据策略添加类型信息
        if (strategy.isWithTypeInfo()) {
            objectMapper.activateDefaultTyping(
                    objectMapper.getPolymorphicTypeValidator(),
                    ObjectMapper.DefaultTyping.NON_FINAL,
                    JsonTypeInfo.As.PROPERTY
            );
        }

        return objectMapper;
    }

}
